package com.ckd.flyingtrip.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 共享 资源
 * @author wzz
 *
 */
public class PrivateShardedPreference {
	private static SharedPreferences sp;
	private static Editor editor;
	private static PrivateShardedPreference pSp;
	private static Context mContext;

	private PrivateShardedPreference() {
		if (null == sp) {
			sp = mContext.getSharedPreferences("shared", Context.MODE_PRIVATE);
		}
		if (null == editor)
			editor = sp.edit();
	}

	public static final PrivateShardedPreference getInstance(Context context) {
		mContext = context;
		if (null == pSp)
			pSp = new PrivateShardedPreference();
		return pSp;
	}

	public boolean putString(String key, String value) {
		editor.putString(key, value);
		return commit();
	}

	public boolean putBoolean(String key, boolean value) {
		editor.putBoolean(key, value);
		return commit();
	}

	public void putInteger(String key, int value) {
		editor.putInt(key, value);
		commit();
	}

	public void putSharedPreference(String key, String[] values) {
		String regularEx = ",";
		String str = "";
		if (values != null && values.length > 0) {
			for (String value : values) {
				str += value;
				str += regularEx;
			}
			editor = sp.edit();
			editor.putString(key, str);
			editor.commit();
		}
	}

	/********* 存储list *********/
	public void putSharedPreferenceList(Context context, String key, List<Map<String, String>> datas) {
		JSONArray mJsonArray = new JSONArray();
		for (int i = 0; i < datas.size(); i++) {
			Map<String, String> itemMap = datas.get(i);
			Iterator<Entry<String, String>> iterator = itemMap.entrySet().iterator();
			JSONObject object = new JSONObject();
			while (iterator.hasNext()) {
				Entry<String, String> entry = iterator.next();
				try {
					object.put(entry.getKey(), entry.getValue());
				} catch (JSONException e) {

				}
			}
			mJsonArray.put(object);
		}

		SharedPreferences sp = context.getSharedPreferences("finals", Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(key, mJsonArray.toString());
		editor.commit();
	}

	/********* 获取list *********/
	public List<Map<String, String>> getSharedPreferenceList(Context context, String key) {
		List<Map<String, String>> datas = new ArrayList<Map<String, String>>();
		SharedPreferences sp = context.getSharedPreferences("finals", Context.MODE_PRIVATE);
		String result = sp.getString(key, "");
		try {
			JSONArray array = new JSONArray(result);
			for (int i = 0; i < array.length(); i++) {
				JSONObject itemObject = array.getJSONObject(i);
				Map<String, String> itemMap = new HashMap<String, String>();
				JSONArray names = itemObject.names();
				if (names != null) {
					for (int j = 0; j < names.length(); j++) {
						String name = names.getString(j);
						String value = itemObject.getString(name);
						itemMap.put(name, value);
					}
				}
				datas.add(itemMap);
			}
		} catch (JSONException e) {

		}
		return datas;
	}

	public boolean remove(String key) {
		if (sp.contains(key)) {
			editor.remove(key);
			return commit();
		}
		return false;
	}

	/**
	 * 清理所有数据
	 */
	public void cleanData() {
		editor.clear();
		commit();
	}

	public String getString(String key, String defValue) {
		return sp.getString(key, defValue);
	}

	public boolean getBoolean(String key, boolean defValue) {
		return sp.getBoolean(key, defValue);
	}

	public int getInteger(String key, int defValue) {
		return sp.getInt(key, defValue);
	}

	public String[] getSharedPreference(String key) {
		String regularEx = ",";
		String[] str = null;
		String values;
		values = sp.getString(key, "");
		str = values.split(regularEx);

		return str;
	}

	private boolean commit() {
		return null != editor && editor.commit();
	}
}